@echo off
REM FastAPI开发环境启动脚本 (Windows)
REM 使用方法: scripts\dev.bat [命令]
REM 命令选项: start, stop, logs, test, lint, format, clean

setlocal enabledelayedexpansion

REM 颜色定义 (Windows CMD)
set "RED=[91m"
set "GREEN=[92m"
set "YELLOW=[93m"
set "BLUE=[94m"
set "NC=[0m"

REM 日志函数
:log_info
echo %BLUE%[INFO]%NC% %~1
goto :eof

:log_success
echo %GREEN%[SUCCESS]%NC% %~1
goto :eof

:log_warning
echo %YELLOW%[WARNING]%NC% %~1
goto :eof

:log_error
echo %RED%[ERROR]%NC% %~1
goto :eof

REM 项目根目录
set "PROJECT_ROOT=%~dp0.."
cd /d "%PROJECT_ROOT%"

REM 检查uv是否安装
uv --version >nul 2>&1
if errorlevel 1 (
    call :log_error "uv 未安装，请先安装 uv"
    call :log_info "安装命令: powershell -c \"irm https://astral.sh/uv/install.ps1 ^| iex\""
    exit /b 1
)

REM 检查环境变量文件
if not exist ".env" (
    if exist ".env.example" (
        call :log_warning ".env 文件不存在，从 .env.example 复制"
        copy ".env.example" ".env" >nul
        call :log_warning "请编辑 .env 文件并设置正确的环境变量"
    ) else (
        call :log_error ".env 和 .env.example 文件都不存在"
        exit /b 1
    )
)

REM 获取命令参数
set "COMMAND=%~1"
if "%COMMAND%"=="" set "COMMAND=start"

REM 主逻辑
if "%COMMAND%"=="start" goto start_dev
if "%COMMAND%"=="stop" goto stop_dev
if "%COMMAND%"=="logs" goto show_logs
if "%COMMAND%"=="test" goto run_tests
if "%COMMAND%"=="lint" goto run_lint
if "%COMMAND%"=="format" goto format_code
if "%COMMAND%"=="clean" goto clean_env
if "%COMMAND%"=="help" goto show_help
if "%COMMAND%"=="--help" goto show_help
if "%COMMAND%"=="-h" goto show_help

call :log_error "未知命令: %COMMAND%"
goto show_help

:start_dev
call :log_info "启动开发环境..."

REM 安装依赖
call :log_info "安装依赖..."
uv sync --extra dev
if errorlevel 1 (
    call :log_error "依赖安装失败"
    exit /b 1
)

REM 创建日志目录
if not exist "logs" mkdir logs

REM 启动应用
call :log_success "启动FastAPI应用..."
call :log_info "应用地址: http://localhost:8088"
call :log_info "API文档: http://localhost:8088/docs"
call :log_info "按 Ctrl+C 停止应用"

uv run uvicorn main:app --reload --host 0.0.0.0 --port 8088
goto :eof

:stop_dev
call :log_info "停止开发环境..."

REM 杀死可能残留的进程
taskkill /f /im python.exe >nul 2>&1
taskkill /f /im uvicorn.exe >nul 2>&1

call :log_success "开发环境已停止"
goto :eof



:show_logs
call :log_info "显示应用日志..."
if exist "logs\app.log" (
    type "logs\app.log"
) else (
    call :log_warning "日志文件不存在: logs\app.log"
)
goto :eof

:run_tests
call :log_info "运行测试..."

REM 确保测试依赖已安装
uv sync --extra dev
if errorlevel 1 (
    call :log_error "依赖安装失败"
    exit /b 1
)

REM 运行测试
uv run pytest -v --cov=src --cov-report=term-missing
if errorlevel 1 (
    call :log_error "测试失败"
    exit /b 1
)

call :log_success "测试完成"
goto :eof

:run_lint
call :log_info "运行代码检查..."

REM 确保开发依赖已安装
uv sync --extra dev
if errorlevel 1 (
    call :log_error "依赖安装失败"
    exit /b 1
)

REM 运行flake8
call :log_info "运行 flake8..."
uv run flake8 src tests
if errorlevel 1 (
    call :log_error "flake8 检查失败"
    exit /b 1
)

REM 运行mypy
call :log_info "运行 mypy..."
uv run mypy src
if errorlevel 1 (
    call :log_error "mypy 检查失败"
    exit /b 1
)

call :log_success "代码检查完成"
goto :eof

:format_code
call :log_info "格式化代码..."

REM 确保开发依赖已安装
uv sync --extra dev
if errorlevel 1 (
    call :log_error "依赖安装失败"
    exit /b 1
)

REM 运行black
call :log_info "运行 black..."
uv run black src tests
if errorlevel 1 (
    call :log_error "black 格式化失败"
    exit /b 1
)

REM 运行isort
call :log_info "运行 isort..."
uv run isort src tests
if errorlevel 1 (
    call :log_error "isort 格式化失败"
    exit /b 1
)

call :log_success "代码格式化完成"
goto :eof

:clean_env
call :log_info "清理环境..."

REM 清理Python缓存
for /d /r . %%d in (__pycache__) do @if exist "%%d" rd /s /q "%%d" 2>nul
for /r . %%f in (*.pyc) do @if exist "%%f" del /q "%%f" 2>nul
for /d /r . %%d in (.pytest_cache) do @if exist "%%d" rd /s /q "%%d" 2>nul
for /d /r . %%d in (.mypy_cache) do @if exist "%%d" rd /s /q "%%d" 2>nul

REM 清理日志文件
if exist "logs" (
    del /q "logs\*.log*" 2>nul
)

call :log_success "环境清理完成"
goto :eof

:show_help
echo FastAPI开发环境管理脚本 (Windows)
echo.
echo 使用方法: %~nx0 [命令]
echo.
echo 可用命令:
echo   start    - 启动开发环境
echo   stop     - 停止开发环境
echo   logs     - 查看应用日志
echo   test     - 运行测试
echo   lint     - 运行代码检查
echo   format   - 格式化代码
echo   clean    - 清理环境
echo   help     - 显示帮助信息
echo.
echo 示例:
echo   %~nx0 start    # 启动开发环境
echo   %~nx0 test     # 运行测试
echo   %~nx0 format   # 格式化代码
goto :eof